<script setup>
import PageContainer from '@/components/PageContainer.vue'
import { ref } from 'vue'
import { getArticleListServer, deleteArticleServer } from '@/api/article.js'
import { Delete, Edit } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'
import ChannelSelect from '@/views/article/component/ChannelSelect.vue'
import { format } from '@/utils/formatTime.js'
import ChannelDrawer from '@/views/article/component/ChannelDrawer.vue'
const articleList = ref([])
const loading = ref(false)
const form = ref(null)
const total = ref(0)
const formModel = ref({
  cate_id: '',
  state: '',
  pagenum: 1,
  pagesize: 3
})
// 获取-文章列表
const goArticleList = async () => {
  loading.value = true
  const res = await getArticleListServer(formModel.value)
  articleList.value = res.data
  total.value = res.total
  loading.value = false
}
goArticleList()
// 点击搜索 --- 一定要重置页码
const goSearch = () => {
  // 重置页码
  formModel.value.pagenum = 1
  goArticleList()
}
// 点击重置 --- 一定要重置页码
const reset = () => {
  // 重置页码
  formModel.value.pagenum = 1
  form.value.resetFields()
  goArticleList()
}
// 编辑文章
const onEditArticle = row => {
  drawer.value.open(row)
}
// 删除文章
const onDeleteArticle = async row => {
  await ElMessageBox.confirm('确定删除该文章吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await deleteArticleServer(row.id)
  await goArticleList()
}
// 分页器改变每页条数
const handleSizeChange = val => {
  // 每页条数变化了,页码就要变回第 1 页
  formModel.value.pagenum = 1
  formModel.value.pagesize = val
  goArticleList()
}
// 分页器改变当前页码
const handleCurrentChange = val => {
  formModel.value.pagenum = val
  goArticleList()
}
// 发布文章
const drawer = ref(null)
const submitArticle = () => {
  drawer.value.open({})
}
// 发布成功后刷新页面
const onSuccess = val => {
  if (val === 'add') {
    formModel.value.pagenum = Math.ceil(total.value / formModel.value.pagesize)
  }
  goArticleList()
}
</script>

<template>
  <!--  组件无需引用因为有安装unplugin-vue-components插件-->
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="submitArticle">发布文章</el-button>
    </template>
    <!--发布文章抽屉开始-->
    <channel-drawer ref="drawer" @success="onSuccess"></channel-drawer>
    <!--发布文章抽屉结尾-->
    <el-form inline ref="form" :model="formModel">
      <el-form-item label="文章分类：" prop="cate_id">
        <!-- 文章分类开始 -->
        <channel-select v-model="formModel.cate_id"></channel-select>
        <!-- 文章分类结尾 -->
      </el-form-item>
      <el-form-item label="发布状态：" prop="state">
        <el-select style="width: 240px" v-model="formModel.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="goSearch">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="articleList" style="width: 100%">
      <el-table-column label="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间">
        <template #default="{ row }">
          {{ format(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination
      v-if="articleList.length !== 0"
      v-model:current-page="formModel.pagenum"
      v-model:page-size="formModel.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      background
      layout="->, jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin: 20px 0"
    />
  </page-container>
</template>

<style scoped>
.channel-select {
  width: 240px;
}
</style>
